package com.xian.ucenter.aliyunoss.service.Impl;

import cn.hutool.core.date.DateTime;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.xian.ucenter.aliyunoss.service.OssService;
import com.xian.ucenter.utils.AliConstantUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String uploadAvatarFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        // 1.在文件名称里面添加随机的唯一值
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        // 2.创建新的文件夹(按照日期存储)
        String dirName = new DateTime().toString("yyyy/MM/dd/");
        String avatarPath = "blog/avatar/";
        return uploadFile(file, fileName, uuid, dirName, avatarPath);
    }

    @Override
    public String uploadPostImage(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        // 1.在文件名称里面添加随机的唯一值
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        // 2.创建新的文件夹(按照日期存储)
        String dirName = new DateTime().toString("yyyy/MM/dd/");
        String avatarPath = "blog/post/images";
        return uploadFile(file, fileName, uuid, dirName, avatarPath);
    }

    @Override
    public String uploadPostCover(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        // 1.在文件名称里面添加随机的唯一值
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        // 2.创建新的文件夹(按照日期存储)
        String dirName = new DateTime().toString("yyyy/MM/dd/");
        String avatarPath = "blog/post/covers";
        return uploadFile(file, fileName, uuid, dirName, avatarPath);
    }

    @Override
    public String uploadProjectCover(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        // 1.在文件名称里面添加随机的唯一值
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        // 2.创建新的文件夹(按照日期存储)
        String dirName = new DateTime().toString("yyyy/MM/dd/");
        String avatarPath = "blog/project/covers";
        return uploadFile(file, fileName, uuid, dirName, avatarPath);
    }

    private String uploadFile(MultipartFile file, String fileName, String uuid, String dirName, String avatarPath) {
        OSS ossClient = new OSSClientBuilder().build(AliConstantUtils.END_POINT, AliConstantUtils.KEY_ID, AliConstantUtils.KEY_SECRET);
        fileName = avatarPath +dirName+uuid+fileName;
        try {
            InputStream inputStream = file.getInputStream();
            ossClient.putObject(AliConstantUtils.BUCKET_NAME, fileName, inputStream);
            return "https://"+AliConstantUtils.BUCKET_NAME+"."+AliConstantUtils.END_POINT+"/"+fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
